package com.zw.goodsservice.common;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 雪花算法封装
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "snowflake")
public class SnowflakeIdGenerator {

    private long workerId;
    private long datacenterId;
    private Snowflake snowflake;

    public SnowflakeIdGenerator() {
    }

    @PostConstruct
    public void init() {
        this.snowflake = IdUtil.getSnowflake(workerId, datacenterId);
    }

    public String nextId() {
        return snowflake.nextIdStr();
    }
}
